package com.website.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.warpper.ResultMap;
import com.website.system.dao.PharmacyMapper;
import com.website.system.model.Pharmacy;
import com.website.system.service.PharmacyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg.ERROR_MSG;
import static com.website.common.controller.BaseController.*;

@Controller
@RequestMapping("manage/pharmacy")
public class PharmacyController {
    @Resource
    private PharmacyService pharmacyService;
    @Resource
    PharmacyMapper pharmacyMapper;
    @RequestMapping("")
    public String pharmacy() { return "manage/pharmacy"; }
    @RequestMapping("/findAll")
    @ResponseBody
    /**
     * 显示所有 药品库信息
     */
    public ResultMap findAll(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(required =false) String pname){
        ResultMap<List<Pharmacy>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Page<Pharmacy> pageInfo = new Page<>(page,limit);
        Wrapper<Pharmacy> wrapper = new EntityWrapper<>();
        wrapper = pname == null ? wrapper : wrapper.like("name", pname);
        List<Pharmacy> list;
        if(pname ==null) {
            list = pharmacyService.selectPage(pageInfo, wrapper.orderBy("pid", true)).getRecords();
        }else{
            list = pharmacyService.selectList(wrapper);
        }
        pharmacyService.selectPage(pageInfo).getRecords();
        resultMap.setCount(pharmacyService.selectCount(wrapper));
        resultMap.setData(list);
        return resultMap;
    }
    /**
     * 查看页面 药库信息
     */
    @RequestMapping("/toread/{pid}")
    public String PharmacyRead(Model model, @PathVariable Integer pid) {
        model.addAttribute("pharmacy",pharmacyService.findById(pid));
        return "manage/pharmacyRead";
    }
    /**
     * 先跳转到修改页面 显示药库信息
     */
    @RequestMapping("/toedit/{pid}")
    public String PharmacytoEdit(Model model, @PathVariable Integer pid) {
        model.addAttribute("pharmacy",pharmacyService.findById(pid));
        return "manage/pharmacyEdit";
    }

    @RequestMapping("/edit")
    public String PharmacyEdit(Pharmacy pharmacy){
        System.out.println("123");
        try {
            pharmacyService.editById(pharmacy);
            return SUCCESS_MSG;
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR_MSG;
        }
    }
    /**
     * 删除drugtype
     */
    @RequestMapping("/deltype/{pid}")
    @ResponseBody
    public Object deltype(@PathVariable Integer pid) {
        int flag=pharmacyService.IfDrugType(pid);
        System.out.println(flag);
        if(flag==0){
            pharmacyMapper.deltype(pid);
            return SUCCESS_MSG;
        }
        else{
           return IF_MSG;
        }
    }

}
